package com.zmkj.cn.sys.service.impl;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.zmkj.cn.comm.vo.RestVo;
import com.zmkj.cn.sys.service.ISysCommonService;

import lombok.extern.slf4j.Slf4j;

/**
 * 公共
 * @author 伍绍宇
 * @date 2021/04/28
 */
@Service
@Slf4j
public class SysCommonServiceImpl implements ISysCommonService {
    @Resource
    private RestTemplate restTemplate;


    @Override
    public RestVo forwardInfo(HttpServletRequest request, String hospitalUrl, MultiValueMap<String, String> params, Map<String,Object> header) {
        // 获取URL
        String forwardUrl = hospitalUrl + request.getServletPath();
        if((boolean) header.get("newUserLogin")){
             forwardUrl = hospitalUrl + "/firstLoginWeChat";
        }
        log.info("forwardUrl---------------" + forwardUrl);
        HttpHeaders headers = new HttpHeaders();
        header.forEach((data,value) -> {
            headers.add(data,value.toString());
        });
        headers.setContentType(MediaType.parseMediaType(request.getContentType()));
        HttpEntity<MultiValueMap<String, String>> formEntity = new HttpEntity<>(params, headers);
        String result = restTemplate.postForObject(forwardUrl, formEntity, String.class);
        log.info("result---------------" + result);
        return RestVo.builder().value(result).build();
    }
    @Override
    public RestVo forwardInfo(HttpServletRequest request, String hospitalUrl, MultiValueMap<String, String> params) {
        return forwardInfo(request, hospitalUrl, params, null);
    }
}
